﻿
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using HDModel;
using HDIDAL;
using DBUtility;
namespace HDDAL
{
    /// <summary>
    /// 数据操作层 dbo.HD_ADMIN.
    /// </summary>
    public class HD_ADMIN : IHD_ADMIN
    {
        #region 基本方法
        /// <summary>
        /// 
        /// </summary>
        public HD_ADMIN()
        {
        }
        #endregion

        #region 实现接口方法

        /// <summary>
        /// 向数据库中插入一条新记录。
        /// </summary>
        /// <param name="_HD_ADMIN">HD_ADMIN实体</param>
        /// <returns>新插入记录的编号</returns>
        public int Create_HD_ADMIN_Insert(HDModel.HD_ADMIN _HD_ADMIN)
        {
            string SQL = "INSERT_HD_ADMIN";
            int res;
            SqlParameter[] _param ={
			new SqlParameter("@ID",SqlDbType.Int),
			new SqlParameter("@NAME",SqlDbType.VarChar),
			new SqlParameter("@PASSWORD",SqlDbType.VarChar),
			new SqlParameter("@TYPE",SqlDbType.VarChar),
			new SqlParameter("@IP",SqlDbType.VarChar),
			new SqlParameter("@PDATE",SqlDbType.DateTime)
			};
            _param[0].Value = _HD_ADMIN.ID;
            _param[1].Value = _HD_ADMIN.NAME;
            _param[2].Value = _HD_ADMIN.PASSWORD;
            _param[3].Value = _HD_ADMIN.TYPE;
            _param[4].Value = _HD_ADMIN.IP;
            _param[5].Value = _HD_ADMIN.PDATE;
            res = (int)SqlHelper.ExecuteScalar(SqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, SQL, _param);
            return res;
        }
        /// <summary>
        /// 向数据库中插入一条新记录。带事务
        /// </summary>
        /// <param name="sp">事务对象</param>
        /// <param name="_HD_ADMIN">HD_ADMIN实体</param>
        /// <returns>新插入记录的编号</returns>
        public int Create_HD_ADMIN_Insert(SqlTransaction sp, HDModel.HD_ADMIN _HD_ADMIN)
        {
            string SQL = "INSERT_HD_ADMIN";
            int res;
            SqlParameter[] _param ={
			new SqlParameter("@ID",SqlDbType.Int),
			new SqlParameter("@NAME",SqlDbType.VarChar),
			new SqlParameter("@PASSWORD",SqlDbType.VarChar),
			new SqlParameter("@TYPE",SqlDbType.VarChar),
			new SqlParameter("@IP",SqlDbType.VarChar),
			new SqlParameter("@PDATE",SqlDbType.DateTime)
			};
            _param[0].Value = _HD_ADMIN.ID;
            _param[1].Value = _HD_ADMIN.NAME;
            _param[2].Value = _HD_ADMIN.PASSWORD;
            _param[3].Value = _HD_ADMIN.TYPE;
            _param[4].Value = _HD_ADMIN.IP;
            _param[5].Value = _HD_ADMIN.PDATE;
            res = (int)SqlHelper.ExecuteScalar(sp, CommandType.StoredProcedure, SQL, _param);
            return res;
        }


        /// <summary>
        /// 向数据表HD_ADMIN更新一条记录。
        /// </summary>
        /// <param name="_HD_ADMIN">_HD_ADMIN</param>
        /// <returns>影响的行数</returns>
        public int Create_HD_ADMIN_Update(HDModel.HD_ADMIN _HD_ADMIN)
        {
            string SQL = "UPDATE_HD_ADMIN";
            SqlParameter[] _param ={
				new SqlParameter("@ID",SqlDbType.Int),
				new SqlParameter("@NAME",SqlDbType.VarChar),
				new SqlParameter("@PASSWORD",SqlDbType.VarChar),
				new SqlParameter("@TYPE",SqlDbType.VarChar),
				new SqlParameter("@IP",SqlDbType.VarChar),
				new SqlParameter("@PDATE",SqlDbType.DateTime)
				};
            _param[0].Value = _HD_ADMIN.ID;
            _param[1].Value = _HD_ADMIN.NAME;
            _param[2].Value = _HD_ADMIN.PASSWORD;
            _param[3].Value = _HD_ADMIN.TYPE;
            _param[4].Value = _HD_ADMIN.IP;
            _param[5].Value = _HD_ADMIN.PDATE;
            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, SQL, _param);
        }
        /// <summary>
        /// 向数据表HD_ADMIN更新一条记录。带事务
        /// </summary>
        /// <param name="sp">事务对象</param>
        /// <param name="_HD_ADMIN">_HD_ADMIN</param>
        /// <returns>影响的行数</returns>
        public int Create_HD_ADMIN_Update(SqlTransaction sp, HDModel.HD_ADMIN _HD_ADMIN)
        {
            string SQL = "UPDATE_HD_ADMIN";
            SqlParameter[] _param ={
				new SqlParameter("@ID",SqlDbType.Int),
				new SqlParameter("@NAME",SqlDbType.VarChar),
				new SqlParameter("@PASSWORD",SqlDbType.VarChar),
				new SqlParameter("@TYPE",SqlDbType.VarChar),
				new SqlParameter("@IP",SqlDbType.VarChar),
				new SqlParameter("@PDATE",SqlDbType.DateTime)
				};
            _param[0].Value = _HD_ADMIN.ID;
            _param[1].Value = _HD_ADMIN.NAME;
            _param[2].Value = _HD_ADMIN.PASSWORD;
            _param[3].Value = _HD_ADMIN.TYPE;
            _param[4].Value = _HD_ADMIN.IP;
            _param[5].Value = _HD_ADMIN.PDATE;
            return SqlHelper.ExecuteNonQuery(sp, CommandType.StoredProcedure, SQL, _param);
        }

        /// <summary>
        /// 删除数据表HD_ADMIN中的一条记录
        /// </summary>
        /// <param name="ID">ID</param>
        /// <returns>影响的行数</returns>
        public int Create_HD_ADMIN_Delete(int ID)
        {
            string SQL = "DELETE_HD_ADMIN";
            SqlParameter[] _param ={
			new SqlParameter("@ID",SqlDbType.Int)			
			};
            _param[0].Value = ID;
            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, SQL, _param);
        }

        /// <summary>
        /// 删除数据表HD_ADMIN中的一条记录，带事务
        /// </summary>
        /// <param name="sp">事务对象</param>
        /// <param name="ID">ID</param>
        /// <returns>影响的行数</returns>
        public int Create_HD_ADMIN_Delete(SqlTransaction sp, int ID)
        {
            string SQL = "DELETE_HD_ADMIN";
            SqlParameter[] _param ={
			new SqlParameter("@ID",SqlDbType.Int)
			};
            _param[0].Value = ID;
            return SqlHelper.ExecuteNonQuery(sp, CommandType.StoredProcedure, SQL, _param);
        }


        /// <summary>
        /// 得到hd_admin 数据实体
        /// </summary>
        /// <param name="row">row</param>
        /// <returns>hd_admin 数据实体</returns>
        public HDModel.HD_ADMIN Populate_HD_ADMIN_FromDr(DataRow row)
        {
            HDModel.HD_ADMIN nObject = new HDModel.HD_ADMIN();
            if (row != null)
            {
                nObject.ID = Convert.ToInt32(row["ID"]);
                nObject.NAME = row["NAME"].ToString();
                nObject.PASSWORD = row["PASSWORD"].ToString();
                nObject.TYPE = row["TYPE"].ToString();
                nObject.IP = row["IP"].ToString();
                nObject.PDATE = (DateTime)row["PDATE"];
            }
            else
            {
                return null;
            }
            return nObject;
        }
        /// <summary>
        /// 根据ID,创建一个HD_ADMIN实体
        /// </summary>
        /// <param name="ID">ID</param>
        /// <returns>HD_ADMIN实体</returns>
        public HDModel.HD_ADMIN Get_HD_ADMIN_Model(int ID)
        {
            SqlParameter[] _param ={
			new SqlParameter("@ID",SqlDbType.Int)
			};
            _param[0].Value = ID;
            string SQL = "select * from HD_ADMIN where ID=@ID";
            DataSet ds = SqlHelper.ExecuteDataset(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL, _param);
            if (ds.Tables.Count < 1)
            {
                ds.Dispose();
                return null;
            }
            else
            {
                if (ds.Tables[0].Rows.Count >= 1)
                {
                    return Populate_HD_ADMIN_FromDr(ds.Tables[0].Rows[0]);
                }
                else
                {
                    return null;
                }
            }
        }
        /// <summary>
        /// 得到数据表HD_ADMIN所有记录
        /// </summary>
        /// <returns>数据集</returns>
        public DataSet Get_HD_ADMIN_All()
        {
            string SQL = "select * from HD_ADMIN";
            return SqlHelper.ExecuteDataset(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL);
        }
        /// <summary>
        /// 检测是否存在根据主键
        /// </summary>
        /// <param name="ID">ID</param>
        /// <returns>是/否</returns>
        public bool IsExistMid(int ID)
        {
            SqlParameter[] _param ={
                                      new SqlParameter("@ID",SqlDbType.Int)
                                  };
            _param[0].Value = ID;
            string SQL = "select Count(*) from HD_ADMIN where ID=@ID";
            int a = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL, _param));
            if (a > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion
    }
}
